package com.xujiang.myblog.network.retrofit;

import com.xujiang.myblog.netserver.UserService;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory;

/**
 * Created by xujiang on 2017/4/14.
 *
 * http://blog.csdn.net/yehui928186846/article/details/51492571
 *
 */

public class RetrofitUtils {


    Retrofit         mRetrofit;
    Retrofit.Builder mRetrofitBuilder;
    Retrofit.Builder mRetrofitBaseUrlBuilder;

    public Retrofit getRetrofit() {
        return mRetrofit;
    }

    private RetrofitUtils(){
    }

    private static RetrofitUtils retrofitUtils;

    public static RetrofitUtils get() {
        if (retrofitUtils == null) {
            retrofitUtils = new RetrofitUtils();
        }
        return retrofitUtils;
    }

    public Retrofit.Builder getRetrofitBuilder() {
        return mRetrofitBuilder;
    }

    public void init(String baseUrl, OkHttpClient client){
        mRetrofitBaseUrlBuilder = new Retrofit.Builder().baseUrl(baseUrl).client(client);
        mRetrofitBuilder = mRetrofitBaseUrlBuilder
               .addConverterFactory(JacksonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create());
        mRetrofit = mRetrofitBuilder.build();
    }

    private UserService mUserService;

    public UserService getUserService() {
        if (mUserService == null) {
            mUserService = RetrofitUtils.get().getRetrofit().create(UserService.class);
        }
        return mUserService;
    }




}
